package com.jketing.entity.ams.base;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jketing.annotation.FieldMeta;
import com.jketing.entity.BaseEntity;

/**
 * Region 行政区域实体
 * 
 * @author  李荣华
 * @version v1.0
 * @date     2017-08-17
 * @since   Jpa 2.1
 */
@JsonIgnoreProperties(value= {"parent","children"})
@Entity
@Table(name = "Region_")
public class Region extends BaseEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4708181028987233921L;
	/**
	 * parent:父级引用
	 */
	private Region parent;
	/**
	 * grade:树型级别
	 */
	private Integer grade;
	/**
	 * routeId:全路径ID值
	 */
	private String routeId;
	/**
	 * routeName:全路径名称值
	 */
	private String routeName;
	/**
	 * code:区域代码
	 */
	@FieldMeta(name="String:code",explain="代码")
	private String code;
	/**
	 * name:区域名称
	 */
	@FieldMeta(name="String:code",explain="名称")
	private String name;
	/**
	 * shortName:区域简称
	 */
	@FieldMeta(name="String:shortName",explain="简称")
	private String shortName;
	/**
	 * postCode:邮政编码
	 */
	@FieldMeta(name="String:postCode",explain="邮政编码")
	private String postCode;
	/**
	 * areaCode:电话区号
	 */
	@FieldMeta(name="String:areaCode",explain="电话区号")
	private String areaCode;
	/**
	 * remark:备注
	 */
	private String remark; 
	/**
	 * children:下级集合
	 */
	private Set<Region> children = new HashSet<Region>();
	
	/**
	 * parent 由ParentId引用的上级对象
	 */
	@ManyToOne
	@JoinColumn(name = "ParentId")
	public Region getParent() {
		return parent;
	}
	public void setParent(Region parent) {
		this.parent = parent;
	}

	/**
	 *  grade 级别（在树形结构中的层次）
	 */
	@Column(name="grade_")
	public Integer getGrade() {
		return grade;
	}
	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	/**
	 * routeId 行政区域全路径ID值
	 * 
	 * 如本级ID为4上级ID为2，2的上级为1
	 * 则此routeId值为：1/2/4
	 */
	@Column(columnDefinition="text")
	public String getRouteId() {
		return routeId;
	}
	public void setRouteId(String routeId) {
		this.routeId = routeId;
	}

	/**
	 * routeName 行政区域全路径名称值
	 * 如本级名称为会泽县，会泽县上级为曲靖市
	 * 则此routeName值为：云南省/曲靖市/会泽县
	 */
	@Column(columnDefinition="text")
	public String getRouteName() {
		return routeName;
	}
	public void setRouteName(String routeName) {
		this.routeName = routeName;
	}
	
	/**
	 * code 行政区域代码
	 */
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	/**
	 * name 机构全称
	 */
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * shortName 行政区域简称
	 */
	public String getShortName() {
		return shortName;
	}
	public void setShortName(String shortName) {
		this.shortName = shortName;
	}
	
	/**
	 * postCode 邮政编码
	 */
	public String getPostCode() {
		return postCode;
	}
	public void setPostCode(String postCode) {
		this.postCode = postCode;
	}
	
	/**
	 * areaCode 电话区号
	 */
	public String getAreaCode() {
		return areaCode;
	}
	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}
	
	/**
	 * remark 备注
	 * @return
	 */
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	/**
	 * children 本机构下直接所属下级机构列表
	 */
	@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@OrderBy(value = "id ASC")
	public Set<Region> getChildren() {
		return children;
	}
	public void setChildren(Set<Region> children) {
		this.children = children;
	}
	
	/**
	 * 增加下级组织机构
	 * @param organ
	 */
	public void addOrgan(Region obj) {
		if (!this.children.contains(obj)) {
			this.children.add(obj);
			obj.setParent(this);
		}
	}	

	/**
	 * 删除下级组织机构
	 * @param organ
	 */
	public void removeOrgan(Region obj) {
		if (this.children.contains(obj)) {
			obj.setParent(null);
			this.children.remove(obj);
		}
	}

	/**
	 * 	equals 判断实例是否相等
	 * 
	 * 	是本类实例并且实例id值相等返回true
	 * 	其它情况返回false
	 * 
	 * 	@param object 其它实例
	 */
	@Override
	public boolean equals(Object object) {
		if (!(object instanceof Region)) {
			return false;
		}
		Region other = (Region) object;
		if(!id.equals(other.id)){
			return false;
		}
		return true;
	}
}
